<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/5
 * Time: 16:28
 */

namespace App\Game\Traits;

use App\Models\GameList;
use Crypt_RSA;
use Carbon\Carbon;

trait FyTrait
{
    //转出游戏
    public function platGameList()
    {
        $url = $this->gameUrl . self::API_GAME_LIST;

        $output = $this->request('POST', $url, [], $this->header);
        $output = json_decode($output, true);
        /*foreach ($result['data'] as $item) {
                    $cn_name = array_first($item['nameset'], function ($value) {
                        if ($value['lang'] == 'zh-cn') {
                            return $value;
                        }

                    }
                               )['name'];

                    $en_name = array_first($item['nameset'], function ($value) {
                        if ($value['lang'] == 'en') {
                            return $value;
                        }

                    }
                               )['name'];

                    GameList::create([
                            'unique_key' => Uuid::uuid4(),
                            'main_game_plat_code' => 'cq9',
                            'cn_name' => $cn_name,
                            'en_name' => $en_name,
                            'pc_game_code' => $item['gamecode'],
                            'mobile_game_code' => $item['gamecode'],
                            'record_match_code' => $item['gamecode'],
                            'type' => 1,
                            'category' => 2,
                            'line_num' => 9,
                            'status' => 1,
                        ]
                    );
                }*/
        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1 && $output['msg'] == '游戏分类') {
                return ['success' => true, 'data' => $output['info']['list'], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '获取游戏失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2019/2/23 16:47
     * Administrator
     * fyRequest
     * fy请求
     * @param string $method
     * @param $url
     * @param $params
     * @return mixed
     */
    public function fyRequest($method='POST',$url,$params)
    {
        $this->header = [
            "Authorization" => $this->apiKey,
        ];

        $output = $this->request($method, $url, $params, $this->header);
        return json_decode($output, true);
    }

    /**
     * gameapi
     * 2018/9/24 17:35
     * Administrator
     * apiDataParse
     * 字段解析
     * @param array $apiData
     * @param array $data
     * @return array
     */
    public function apiDataParse(array $apiData, array $data)
    {
        $data = $this->statusParse($apiData, $data);
        return $data;
    }

    /**
     * gameapi
     * 2018/9/24 17:36
     * Administrator
     * statusParse
     * 解析状态字段
     * @param array $apiData
     * @param array $data
     * @return array
     */
    private function statusParse(array $apiData, array $data)
    {
        if (array_key_exists('Status', $apiData)) {
            switch ($apiData['Status']) {
                case 'None':
                    $data['status'] = 0;
                    break;
                case 'Revoke':
                    $data['status'] = -1;
                    break;
                case 'Win':
                    $data['status'] = 1;
                    break;
                case 'Lose':
                    $data['status'] = 1;
                    break;
                case 'WinHalf':
                    $data['status'] = 1;
                    break;
                case 'LoseHalf':
                    $data['status'] = 1;
                    break;
                case 'Settlement':
                    $data['status'] = 0;
                    break;
                case 'Cancel':
                    $data['status'] = -1;
                    break;
                default:
                    $data['status'] = 1;
                    break;
            }
        }
        return $data;
    }

    public function getGameList($items)
    {
        if (is_array($items)) {
            if (count($items) >= 1) {
                foreach ($items as $item) {
                    $game = GameList::where('main_game_plat_code', $this->platName)->where('cn_name', $item['Category'])->first();
                    if (empty($game)) {
                        $game['main_game_plat_code'] = $this->platName;
                        $game['cn_name'] = $item['Category'];
                        $game['record_match_code'] = $item['CateID'];
                        $game['type'] = 'other';
                        $game['category'] = 'e-sports';
                        $game['line_num'] = '9';
                        $game['status'] = 1;
                        GameList::create($game);
                    }
                }
            }
        }
    }
}